/**
 * 购货订单js脚本 
 */
corpApp.controller('controller',['$scope', 'upserve', 'ngDialog', function($scope, upserve, ngDialog){
	//excel名称
	$scope.fileName ='计划录入列表';
	//状态数
	$scope.statusData01 = 0;
	$scope.statusData02 = 0;
	$scope.statusData03 = 0;
	$scope.statusData04 = 0;
	$scope.statusData05 = 0;
	$scope.statusData06 = 0;
	$scope.statusData07 = 0;
	$scope.statusData08 = 0;
	//时间控件
	$scope.startOpend = false;
	$scope.endOpend = false;
	$scope.tipShow = false;
	var d = new Date();
	d.setDate(d.getDate()-7);
	$scope.startDate = d.format('yyyy-MM-dd');
	$scope.openStartDatePicker = function($event){
		 $event.preventDefault();
         $event.stopPropagation();
         $scope.startOpend =  !$scope.startOpend;
	}
	
	$scope.openEndDatePicker = function($event){
		 $event.preventDefault();
         $event.stopPropagation();
         $scope.endOpend = !$scope.endOpend;
	}
	
	$scope.grid = {};
	//搜索条件
	$scope.condition = {
			OrderStatusCode:'',
			BillDateStart:'',
			BillDateEnd:'',
			VendorName:'',
			VendorCode:'',
			PersonCode:'',
			VendorModeCode:'',
			OrderCode:''
	};
	//订单状态
	$scope.condition.OrderStatusCode = angular.element('#txtStatusCode').val();
	
	/**
	 * 主表列
	 */
	$scope.columns = [
		{headerAttributes:{style:"text-align:center"},field:'',title:'操作',width:100 , hidden:false, attributes:{style:"text-align:center"}, template:function(e){
			var template = '<a style="color:#f89d3b" href="'+$path$+'/admin/JN_PlanningInput_Edit" >修改 </a>';
				template +="&nbsp;|&nbsp;&nbsp;<a href='' style='color:#f89d3b' ng-click='del(dataItem)'>删除 </a>";
			return template;
			},
			headerAttributes:{style:"text-align:center"},footerTemplate: "<div style='text-align:left'>合计</div>"
		},
		{headerAttributes:{style:"text-align:center"},field:'PlanName',title:'计划名称', width:window.noWidth, hidden:false},
		{headerAttributes:{style:"text-align:center"},field:'Subject',title:'科目', width:100, hidden:false},
		{headerAttributes:{style:"text-align:center"},field:'ProjectName',title:'项目名称', width:window.longNameWidth, hidden:false},
		{headerAttributes:{style:"text-align:center"},field:'FundSource',title:'资金来源' , hidden:false},
		{headerAttributes:{style:"text-align:center"},field:'InTaxMoney',title:'金额(万元)', width:window.moneyWidth, hidden:false, attributes:{style:"text-align:right"},
			template:function(e){if(e.InTaxMoney==0){return ''}else{return kendo.format(window.numberFormatStr,e.InTaxMoney)}},  
			aggregates: ["sum"], footerTemplate: "<div style='text-align:right'>#=kendo.format('"+window.moneyFormatStr+"',sum)#</div>"
		},
		{headerAttributes:{style:"text-align:center"},field:'ProjectSort',title:'项目类别', width:window.smallWidth, hidden:false},
		{headerAttributes:{style:"text-align:center"},field:'ManageOrganize',title:'管理机构', width:window.smallWidth, hidden:false},
		{headerAttributes:{style:"text-align:center"},field:'Year',title:'年度', width:window.smallWidth, hidden:false}
	];
	/**
	 * 合计项
	 */
	$scope.aggregate = [
	   {field:'InTaxMoney', aggregate:'sum'},
	  
    ];
	/**
	 * 列表参数设置
	 */
	$scope.options = {
			excelExport: function(e) {
			    var sheet = e.workbook.sheets[0];
			    var reg = new RegExp('>(.*)<');
			    var length = sheet.rows.length;
			    angular.forEach(sheet.rows, function(row){
			    	if(row.type==='group-footer'||row.type==='footer'){
			    		angular.forEach(row.cells, function(cell){
			    			var matchs = reg.exec(cell.value);
			    			if(matchs!=null){
			    				cell.value = matchs[1];
			    				if(cell.value=='合计'||cell.value=='小计'){
			    					cell.hAlign = 'left';
			    				}else{
			    					cell.hAlign = 'right';
			    				}
			    			}
			    		});
			    	}
			    });
			},
			excel: {
	            allPages: true,
	            fileName:$scope.fileName + '.xlsx'
	        },
	        dataBound: function(e) {
            	var grid = e.sender;
            	var gridData = grid.dataSource.view();
                for (var i = 0; i < gridData.length; i++) {
                	//用于修改记录的背景色
                    if (gridData[i].ClosedownFlag == '1') {
                        grid.table.find("tr[data-uid='" + gridData[i].uid + "']").css("color", 'green');
                    }
                    if (gridData[i].ClosedownFlag == '2') {
                        grid.table.find("tr[data-uid='" + gridData[i].uid + "']").css("color", 'blue');
                    }
                }
            },
			resizable:true,
			sortable: true,
		    selectable:true,
		    height: 550,
		    pageable:true,
		    columns:$scope.columns
		   
	};
	/**
	 * 主表数据参数设置
	 */
	$scope.dataSource = new kendo.data.DataSource({
	     data:[],
		 page: 1,
		 pageSize: 20,
		 aggregate:$scope.aggregate,
		 schema:{
			 model:{
				 fields:{
					 DeliveryDate:{
						 type:'date'
					 }
				 }
			 }
		 }
	});
	
	//计划
	$scope.Plans = [];
	$scope.getPlans = function(){
		$scope.Plans=[{
			"PlanCode":"1",
			"PlanName":"第一批"
		},
		{
			"PlanCode":"2",
			"PlanName":"第二批"
		},
		{
			"PlanCode":"3",
			"PlanName":"第三批"
		}]
	};
	$scope.getPlans();
	//资金来源
	$scope.Sources = [];
	$scope.getSources = function(){
		$scope.Sources=[{
			"SourceCode":"1",
			"SourceName":"成品油税"
		},
		{
			"SourceCode":"2",
			"SourceName":"营业税"
		},
		{
			"SourceCode":"3",
			"SourceName":"教育收入"
		}]
	};
	$scope.getSources();
	//项目类别
	$scope.ProjectSorts = [];
	$scope.getProjectSorts = function(){
		$scope.ProjectSorts=[{
			"ProjectSortCode":"1",
			"ProjectSortName":"日常养护费"
		},
		{
			"ProjectSortCode":"2",
			"ProjectSortName":"改建费"
		},
		{
			"ProjectSortCode":"3",
			"ProjectSortName":"建造费"
		}]
	};
	$scope.getProjectSorts();
	
	
	//科目代码参照
	$scope.showSubjectCodeSelectDlg = function(){
		
		var dialog = ngDialog.open({
			templateUrl: $path$ + "/public/template/Help_Subject_Conditionfilter.html",
			width:650,
			scope:$scope,
			controller:['$scope', 'upserve', function($scope, upserve){
				$scope.data = {};
				$scope.grid = {};
				
				$scope.columns = [
                    {field:'SubjectCode',title:'科目代码',width:100 , hidden:false},
				 	{field:'SubjectName ',title:'科目代码名称',width:100 , hidden:false}
				];
				
				$scope.options = {
				 	resizable:true,
				 	sortable: true,
				 	selectable:true,
				 	height: 300,
				 	pageable:true,
				 	groupable:false,
				 	columns:$scope.columns,
				 	change:function(){
				 		$scope.data = this.dataItem(this.select()[0]);
				 	},
				 	dataBound:function(e){
				 		var grid = this;
				 		angular.element(this.tbody).find('tr').dblclick(function(e, grid){
				 			$scope.ok();
				 		});
				 	}
				 };
				
				 $scope.dataSource = new kendo.data.DataSource({
				 	data:[],
				 	page: 1,
				 	pageSize:window.refPageSize
				});
				 $scope.subject=[{
					 "SubjectCode":"1",
					 "SubjectName":"城市公园建设"
				 },
				 {
					 "SubjectCode":"2",
					 "SubjectName":"道路桥梁支出"
				 },
				 {
					 "SubjectCode":"3",
					 "SubjectName":"公路运输支出"
				 }]
				 $scope.dataSource.data($scope.subject);
				 $scope.ok = function(){
					 console.log($scope.data.SubjectCode)
					 $scope.condition.SubjectCode=$scope.data.SubjectCode;
						$scope.$apply();
					 dialog.close();
				 }
				 $scope.cancel = function(){
					 dialog.close();
				 }
			}]
		});
	}
	//管理机构
	//科目代码参照
	$scope.showManagerCodeSelectDlg = function(){
		
		var dialog = ngDialog.open({
			templateUrl: $path$ + "/public/template/Help_Manager_Conditionfilter.html",
			width:650,
			scope:$scope,
			controller:['$scope', 'upserve', function($scope, upserve){
				$scope.data = {};
				$scope.grid = {};
				
				$scope.columns = [
                    {field:'ManagerCode',title:'管理机构代码',width:100 , hidden:false},
				 	{field:'ManagerName ',title:'管理机构名称',width:100 , hidden:false}
				];
				
				$scope.options = {
				 	resizable:true,
				 	sortable: true,
				 	selectable:true,
				 	height: 300,
				 	pageable:true,
				 	groupable:false,
				 	columns:$scope.columns,
				 	change:function(){
				 		$scope.data = this.dataItem(this.select()[0]);
				 	},
				 	dataBound:function(e){
				 		var grid = this;
				 		angular.element(this.tbody).find('tr').dblclick(function(e, grid){
				 			$scope.ok();
				 		});
				 	}
				 };
				
				 $scope.dataSource = new kendo.data.DataSource({
				 	data:[],
				 	page: 1,
				 	pageSize:window.refPageSize
				});
				 $scope.subject=[{
					 "ManagerCode":"1",
					 "ManagerName":"税务局"
				 },
				 {
					 "ManagerCode":"2",
					 "ManagerName":"交通局"
				 },
				 {
					 "ManagerCode":"3",
					 "ManagerName":"环保局"
				 }]
				 $scope.dataSource.data($scope.subject);
				 $scope.ok = function(){
					 console.log($scope.data.SubjectCode)
					 $scope.condition.ManagerCode=$scope.data.ManagerCode;
						$scope.$apply();
					 dialog.close();
				 }
				 $scope.cancel = function(){
					 dialog.close();
				 }
			}]
		});
	}
	/**
	 * 改变搜索状态,刷新数据
	 */
	$scope.changeQueryStatus = function(status){
		$scope.condition.OrderStatusCode = status;
		$scope.loadData();
	}
	/**
	 * 加载主表数据
	 */
	$scope.loadData = function(){
		//kendo.ui.progress(angular.element("#loading"), true);
		
		data=[{
			"PlanName":"第一批",
			"Subject":"公路运输支出",
			"ProjectName":"省道G001改建工程",
			"FundSource":"成品油税",
			"InTaxMoney":400,
			"ProjectSort":"日常养护费",
			"ManageOrganize":"省公路局",
			"Year":"2015",
		},
		{
			"PlanName":"第二批",
			"Subject":"道路桥梁支出",
			"ProjectName":"金溪大桥改造",
			"FundSource":"成品油税",
			"InTaxMoney":2000,
			"ProjectSort":"日常养护费",
			"ManageOrganize":"省水利局",
			"Year":"2015",
		},
		{
			"PlanName":"第三批",
			"Subject":"城市公园建设",
			"ProjectName":"中央公园建设",
			"FundSource":"成品油税",
			"InTaxMoney":300,
			"ProjectSort":"日常养护费",
			"ManageOrganize":"省环保局",
			"Year":"2015",
		},
		]
		$scope.dataSource.data(data);
		
		
//		$scope.condition.BillDateStart = upserve.isNoValue($scope.startDate)?'':new Date($scope.startDate).format("yyyy-MM-dd");
//		$scope.condition.BillDateEnd = upserve.isNoValue($scope.endDate)?'':new Date($scope.endDate).format("yyyy-MM-dd");
//		upserve.post($path$+"/admin/jNFactoryOrderViewJSON", $scope.condition, function(data){
//			$scope.dataSource.data(data);
//			kendo.ui.progress(angular.element("#loading"), false);
//		});
	}
	$scope.loadData();
	

    //获取状态的数量
    $scope.getStatusData = function(){
    	
    	$scope.statusData03=5
    	$scope.statusData02 =6
//		upserve.get({
//            url: $path$+"/admin/jNFactoryOrderStatusDetail",
//            success: function (data) {
//            	angular.forEach(data, function(val){
//            		if(val.OrderStatusCode.trim()=='01'){
//            			$scope.statusData01 = val.OrderStatusCount;
//            		}
//            		if(val.OrderStatusCode.trim()=='02'){
//            			$scope.statusData02 = val.OrderStatusCount;
//            		}
//            		if(val.OrderStatusCode.trim()=='03'){
//            			$scope.statusData03 = val.OrderStatusCount;
//            		}
//            		if(val.OrderStatusCode.trim()=='04'){
//            			$scope.statusData04 = val.OrderStatusCount;
//            		}
//            		if(val.OrderStatusCode.trim()=='05'){
//            			$scope.statusData05 = val.OrderStatusCount;
//            		}
//            		if(val.OrderStatusCode.trim()=='06'){
//            			$scope.statusData06 = val.OrderStatusCount;
//            		}
//            		if(val.OrderStatusCode.trim()=='07'){
//            			$scope.statusData07 = val.OrderStatusCount;
//            		}
//            		if(val.OrderStatusCode.trim()=='08'){
//            			$scope.statusData08 = val.OrderStatusCount;
//            		}
//            	});
//            }
//		 })
	};
	 $scope.getStatusData();
	 
	 $scope.exportToExcel = function(){
		 	if($scope.grid.dataSource.data().length==0){
	    		upserve.tip('当前没有需要导出的数据', showTime);
	    		return ;
	    	}
	    	$scope.grid.saveAsExcel();
	    }
	 
	 $scope.del = function(){
		 upserve.confirm('确认要删除此条数据吗？', function(result){
		 });
	 }
}]);

